﻿using System;
using System.Collections.Generic;

using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Documents;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Markup;
using Windows.UI.Xaml.Media;

// The Templated Control item template is documented at https://go.microsoft.com/fwlink/?LinkId=234235

namespace 纸片人.win2dEffectControl
{
    [ContentProperty (Name ="Content")]
    public  class EffectContentControl : Control
    {
        /// <summary>
        /// 获取或设置Content的值
        /// </summary>  
        public object Content
        {
            get { return (object)GetValue(ContentProperty); }
            set { SetValue(ContentProperty, value); }
        }

        /// <summary>
        /// 标识 Content 依赖属性。
        /// </summary>
        public static readonly DependencyProperty ContentProperty =
            DependencyProperty.Register("Content", typeof(object), typeof(EffectContentControl), new PropertyMetadata(null, OnContentChanged));

        private static void OnContentChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
        {
            EffectContentControl target = obj as EffectContentControl;
            object oldValue = (object)args.OldValue;
            object newValue = (object)args.NewValue;
            if (oldValue != newValue)
                target.OnContentChanged(oldValue, newValue);
        }

        protected virtual void OnContentChanged(object oldValue, object newValue)
        {
            
        }



        /// <summary>
        /// 获取或设置ContentTemplate的值
        /// </summary>  
        public DataTemplate ContentTemplate
        {
            get { return (DataTemplate)GetValue(ContentTemplateProperty); }
            set { SetValue(ContentTemplateProperty, value); }
        }

        /// <summary>
        /// 标识 ContentTemplate 依赖属性。
        /// </summary>
        public static readonly DependencyProperty ContentTemplateProperty =
            DependencyProperty.Register("ContentTemplate", typeof(DataTemplate), typeof(EffectContentControl), new PropertyMetadata(null, OnContentTemplateChanged));

        private static void OnContentTemplateChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
        {
            EffectContentControl target = obj as EffectContentControl;
            DataTemplate oldValue = (DataTemplate)args.OldValue;
            DataTemplate newValue = (DataTemplate)args.NewValue;
            if (oldValue != newValue)
                target.OnContentTemplateChanged(oldValue, newValue);
        }

        protected virtual void OnContentTemplateChanged(DataTemplate oldValue, DataTemplate newValue)
        {

        }


        /// <summary>
        /// 当前位置
        /// </summary>
        public Point ControlPoint { get; set; }

        private Grid header;
        private ContentPresenter contentContainer;
        private Button deleteButton;
        /// <summary>
        /// 拖动事件
        /// </summary>
        public event ManipulationDeltaEventHandler HeaderDelta;
        /// <summary>
        /// 拖动完成事件
        /// </summary>
        public event ManipulationCompletedEventHandler HeaderDeltaCompleted;

        public event RoutedEventHandler DeleteClick;

        public EffectContentControl()
        {
            this.DefaultStyleKey = typeof(EffectContentControl);
        }

        protected override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            //标题
            header = GetTemplateChildFormName<Grid>(nameof(header));
            //内容
            contentContainer = GetTemplateChildFormName<ContentPresenter>(nameof(contentContainer));

            header.ManipulationMode = ManipulationModes.TranslateX | ManipulationModes.TranslateY;
            header.ManipulationDelta += Header_ManipulationDelta;
            header.ManipulationCompleted += Header_ManipulationCompleted;

            deleteButton = GetTemplateChildFormName<Button>(nameof(deleteButton));
            deleteButton.Click += DeleteButton_Click;
            this.ManipulationMode = ManipulationModes.TranslateInertia;
            this.ManipulationDelta += ContentContainer_ManipulationDelta;
        }

        private void ContentContainer_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
        {
            e.Handled = true;
        }

        private void DeleteButton_Click(object sender, RoutedEventArgs e)
        {
            DeleteClick?.Invoke(this, e);
        }

        private void Header_ManipulationCompleted(object sender, ManipulationCompletedRoutedEventArgs e)
        {
            HeaderDeltaCompleted?.Invoke(this, e);
        }

        private void Header_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
        {
            HeaderDelta?.Invoke(this,e);
        }

        protected T GetTemplateChildFormName<T>(string name = "") where T : DependencyObject
        {

            var element = GetTemplateChild(name);
            return (T)element;
        }
    }
}
